package com.wld.xm.framework.core.util;

import com.wld.xm.framework.core.common.WebContent;
import com.wld.xm.framework.core.entity.Result;
import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncodingAttributes;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

/**
 * @author hxj
 * @date 2018/5/5 0005  10:57
 */
public class FileUtil {
    public static String convertEscape(String fileDir) {
        return fileDir.replace("\\", "/");
    }

    /**
     * pcm转wav
     *
     * @param src
     * @param target
     * @throws Exception
     */
    public static void convertAudioFiles(String src, String target) throws Exception {
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(target);
        // 计算长度
        byte[] buf = new byte[1024 * 4];
        int size = fis.read(buf);
        int PCMSize = 0;
        while (size != -1) {
            PCMSize += size;
            size = fis.read(buf);
        }
        fis.close();

        WaveHeader header = new WaveHeader();
        header.fileLength = PCMSize + (44 - 8);
        header.FmtHdrLeth = 16;
        header.BitsPerSample = 16;
        header.Channels = 1;
        header.FormatTag = 1;
        header.SamplesPerSec = 16000;
        header.BlockAlign = (short) (header.Channels * header.BitsPerSample / 8);
        header.AvgBytesPerSec = header.BlockAlign * header.SamplesPerSec;
        header.DataHdrLeth = PCMSize;

        byte[] h = header.getHeader();
        assert h.length == 44;
        // write header
        fos.write(h, 0, h.length);
        // write data stream
        fis = new FileInputStream(src);
        size = fis.read(buf);
        while (size != -1) {
            fos.write(buf, 0, size);
            size = fis.read(buf);
        }
        fis.close();
        fos.close();
    }

    /**
     * wav转mp3
     *
     * @param source
     * @param desFileName
     * @return
     * @throws Exception
     */
    public static File convertWav2mp3(String source, String desFileName) throws Exception {
        File wav = new File(source);
        File target = new File(desFileName);
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libmp3lame");
        audio.setBitRate(new Integer(36000)); // 音频比率 MP3默认是1280000
        audio.setChannels(new Integer(2));
        audio.setSamplingRate(new Integer(44100));
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp3");
        attrs.setAudioAttributes(audio);
        Encoder encoder = new Encoder();
        encoder.encode(wav, target, attrs);
        return target;
    }


    //文件类型（1、商品图片，2、店铺图片，3、证件图片，4、个人头像，5、视屏，6、其它）
    public static final String TYPE_GOODSIMG = "1";
    public static final String TYPE_STOREIMG = "2";
    public static final String TYPE_CERTIFICIMG = "3";
    public static final String TYPE_PERSONALIMG = "4";
    public static final String TYPE_VIDEO = "5";
    public static final String TYPE_OTHER = "6";

    private static final Map<String, String> FILETYPES = new HashMap<>();

    static {
        FILETYPES.put("1", "GoodsImg");
        FILETYPES.put("2", "StoreImg");
        FILETYPES.put("3", "CertificImg");
        FILETYPES.put("4", "PersonalImg");
        FILETYPES.put("5", "Video");
        FILETYPES.put("6", "Other");
    }

}
